<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Carts extends Base{
	//加入购物车
	public function cartIn()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
		if(!empty(input("param.p_id"))){
			$data['p_id']=input("param.p_id");
		}else{
			return json(['code' => 502, 'msg' => '产品id']);
		}
		if(!empty(input("param.buy_num"))){
			$data['buy_num']=input("param.buy_num");
		}else{
			return json(['code' => 503, 'msg' => '购买数量']);
		}
		if(!empty(input("param.spec_combination_id"))){
			$data['spec_combination_id']=input("param.spec_combination_id");
		}else{
			return json(['code' => 504, 'msg' => '规格参数id']);
		}
        $uid=UserCrypt::do_mdecrypt($token);
        $isIn=Db::name('carts')->where('user_id',$uid)->where('p_id',$data['p_id'])->where('spec_combination_id',$data['spec_combination_id'])->where('state',1)->find();
        if($isIn){
			return json(['code' => 505, 'msg' => '购物车已加入过此产品']);
        }
		$data['user_id']=$uid;
		$data['state']=1;
		$data['time']=time();
		$data['uptime']=time();
		$res=Db::name('carts')->insert($data);
		if ($res) {
            return json(['code' => 200, 'msg' => '成功！']);
		}else{
            return json(['code' => 500, 'msg' => '失败！']);
		}
	}
	//我的购物车列表
	public function myCarts()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $map = [
            ['a.state', '=', 1]
        ];
        $uid=UserCrypt::do_mdecrypt($token);
        array_push($map,['a.user_id','=',$uid]);
		$res=Db::name('carts')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->join('spec_combination c', 'a.spec_combination_id = c.id','LEFT')->field('a.id,a.user_id,a.p_id,a.spec_combination_id,a.buy_num,b.title,b.img,b.state as p_state,c.combination,c.money,c.num')->where($map)->order('a.id desc')->limit(10*($page-1),10)->select();
		if ($res) {
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
		}else{
            return json(['code' => 500, 'msg' => '查无数据！']);
		}
	}
	//取消(删除)我的购物车
	public function cancelCart()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
            $id=explode(',', input("param.id"));
        }else{
            return json(['code' => 502, 'msg' => 'id呢？']);
        }
        $res= Db::name('carts')->where(array('id'=>$id,'state'=>1))->update(['state'=>2]);
        if ($res) {
            return json(['code' => 200, 'msg' => '成功！']);
		}else{
            return json(['code' => 500, 'msg' => '失败！']);
		}
	}
}